
/*
'===============================================================================
'  Generated From - UsersFirst_BusinessObject_DAAB.vbgen -- Generated 9/3/2010 5:37:12 PM by MyGeneration Version # (1.3.0.3)
' 
'  User.cs
'  Namespace: PFSync
'  Database:pfsync
'  Table:user
'  Columns:
'
		id_user	int
		person_id_h_v	string
		record_id_h_v	string
		patient_id_p_f	string
'
'
'  
'===============================================================================
*/
using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

namespace PFSync
{
	public partial class User
	{
		#region < Private Member Variables >
		
		private int id_user = int.MinValue;
		private string person_id_h_v = string.Empty;
		private string record_id_h_v = string.Empty;
		private string patient_id_p_f = string.Empty;

		#endregion
		
		#region < Constructors >
		public User()
		{
			//Empty Constructor
		}
		public User(int id)
		{
			this.RestoreById(id);
		}
		
		public User(IDataReader reader)
		{
			this.ProcessRecord(reader);
		}
		public User(IDictionary dict)
		{
			this.ProcessDictionary(dict);
		}

		#endregion
		
		#region < Properties >
				
		public int IdUser
		{
			get	{ return id_user; }
			set	{ id_user = value; }
		}
				
		public string PersonIdHV
		{
			get	{ return person_id_h_v; }
			set	{ person_id_h_v = value; }
		}
				
		public string RecordIdHV
		{
			get	{ return record_id_h_v; }
			set	{ record_id_h_v = value; }
		}
				
		public string PatientIdPF
		{
			get	{ return patient_id_p_f; }
			set	{ patient_id_p_f = value; }
		}
				
		#endregion
		private void ProcessDictionary(IDictionary dict)
		{
			if(dict.Contains("idUser"))
				id_user = int.Parse(dict["idUser"].ToString());
			if(dict.Contains("personIdHV"))
				person_id_h_v = (string)dict["personIdHV"];
			if(dict.Contains("recordIdHV"))
				record_id_h_v = (string)dict["recordIdHV"];
			if(dict.Contains("patientIdPF"))
				patient_id_p_f = (string)dict["patientIdPF"];
		
		}
		private void ProcessRecord(IDataReader reader)
		{
		
			id_user = (int)reader["id_user"];
			person_id_h_v = (string)reader["person_id_h_v"];
			record_id_h_v = (string)reader["record_id_h_v"];
			patient_id_p_f = (string)reader["patient_id_p_f"];
		
		}


		public void RestoreById(int id)
		{
			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			
			string sqlCommand = "SELECT * from [user] WHERE id_user=@id_user";
			
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			
			// Add in parameters
			db.AddInParameter(dbCommand, "@id_user", DbType.Int32, id);

			IDataReader myReader = db.ExecuteReader(dbCommand);
			if (myReader.Read())
			{
				this.ProcessRecord(myReader);
			}
			myReader.Close();
			dbCommand.Connection.Close();		
		}
		public static List<User> RestoreAll()
		{
			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			string sqlCommand = "SELECT * from [user]";
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			IDataReader myReader = db.ExecuteReader(dbCommand);
			List<User> all = new List<User>();
			while (myReader.Read())
			{
				all.Add(new User(myReader));
			}
			myReader.Close();
			dbCommand.Connection.Close();
			return all;		
		}
		
		
		
		public void delete()
		{		
				Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");
			if(id_user != int.MinValue){
				string sqlCommand = "DELETE from [user] WHERE id_user=@id_user";
	
				DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			
			// Add in parameters
				db.AddInParameter(dbCommand, "@id_user", DbType.Int32, id_user);

				
				IDataReader reader = db.ExecuteReader(dbCommand);
				reader.Close();
				dbCommand.Connection.Close();
			}
			id_user = int.MinValue;
			person_id_h_v = string.Empty;
			record_id_h_v = string.Empty;
			patient_id_p_f = string.Empty;

		}
		public void save()
		{

			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			string insertCommand = "set nocount on; INSERT INTO [user] (person_id_h_v, record_id_h_v, patient_id_p_f) VALUES (@person_id_h_v, @record_id_h_v, @patient_id_p_f); select @@IDENTITY as ReturnInt";

			string updateCommand = "UPDATE user SET person_id_h_v=@person_id_h_v, record_id_h_v=@record_id_h_v, patient_id_p_f=@patient_id_p_f WHERE id_user=@id_user";


				DbCommand dbCommand;
				string sqlCommand = "";

			if(this.IdUser > 0){
				sqlCommand = updateCommand;
			}else{
				
				sqlCommand = insertCommand;
								
			}
			
			dbCommand = db.GetSqlStringCommand(sqlCommand);	
			
			
			db.AddInParameter(dbCommand, "@id_user", DbType.Int32, IdUser);
			
			db.AddInParameter(dbCommand, "@person_id_h_v", DbType.AnsiString, PersonIdHV);
			
			db.AddInParameter(dbCommand, "@record_id_h_v", DbType.AnsiString, RecordIdHV);
			
			db.AddInParameter(dbCommand, "@patient_id_p_f", DbType.AnsiString, PatientIdPF);

			db.AddOutParameter(dbCommand, "ReturnInt", DbType.Int32, 50);
			
			IDataReader reader = db.ExecuteReader(dbCommand);
			
			if(reader.Read() && this.IdUser <= 0){
				id_user= int.Parse(reader["ReturnInt"].ToString());
			}
			dbCommand.Connection.Close();
		}
	}
}

